package org.example.resourceplatform.controller;

import org.example.resourceplatform.dto.LoginRequest;
import org.example.resourceplatform.dto.RegisterRequest;
import org.example.resourceplatform.entity.User;
import org.example.resourceplatform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpSession;
import java.util.Optional;

@Controller
public class AuthController {

    @Autowired
    private UserService userService;

    // 显示仪表板页面

    // 显示首页
    @GetMapping("/")
    public String showHomePage() {
        return "redirect:/login"; // 重定向到登录页面
    }

    // 显示登录页面
    @GetMapping("/login")
    public String showLoginPage(Model model) {
        return "login";
    }

    // 显示注册页面
    @GetMapping("/register")
    public String showRegisterPage() {
        return "register";
    }
    @GetMapping("/dashboard")
    public String showDashboardPage(HttpSession session, Model model) {
        if (session.getAttribute("user") != null)
            return "dashboard";
        return "redirect:/login";
    }
    // 处理登录请求
    @PostMapping("/login")
    public String login(@ModelAttribute LoginRequest loginRequest,
                        HttpSession session,
                        Model model) {
        System.out.println("=== 登录请求开始 ===");
        System.out.println("邮箱: " + loginRequest.getEmail());
        System.out.println("密码: " + loginRequest.getPassword());

        try {
            Optional<User> userOptional = userService.login(loginRequest);

            if (userOptional.isPresent()) {
                User user = userOptional.get();
                System.out.println("登录成功，用户信息:");
                System.out.println("用户ID: " + user.getUserId());
                System.out.println("姓名: " + user.getName());
                System.out.println("角色: " + user.getRole());
                System.out.println("邮箱: " + user.getEmail());

                // 将用户信息存入session
                session.setAttribute("user", user);
                session.setAttribute("userId", user.getUserId());
                session.setAttribute("userRole", user.getRole());
                session.setAttribute("userName", user.getName());

                System.out.println("Session设置完成，重定向到仪表板");
                System.out.println("=== 登录请求结束 ===");

                return "redirect:/dashboard";
            } else {
                System.out.println("登录失败: 邮箱或密码错误");
                System.out.println("=== 登录请求结束 ===");
                model.addAttribute("error", "邮箱或密码错误");
                return "login";
            }
        } catch (Exception e) {
            System.out.println("登录过程中出现异常: " + e.getMessage());
            e.printStackTrace();
            System.out.println("=== 登录请求结束 ===");
            model.addAttribute("error", "登录失败: " + e.getMessage());
            return "login";
        }
    }

    // 处理注册请求
    @PostMapping("/register")
    public String register(@ModelAttribute RegisterRequest registerRequest, Model model) {
        System.out.println("=== 注册请求开始 ===");
        System.out.println("邮箱: " + registerRequest.getEmail());
        System.out.println("密码: " + registerRequest.getPassword());
        System.out.println("角色: " + registerRequest.getRole());

        try {
            User user = userService.register(registerRequest);
            System.out.println("注册成功，用户ID: " + user.getUserId());
            System.out.println("=== 注册请求结束 ===");

            model.addAttribute("success", "注册成功，请登录");
            return "login";
        } catch (Exception e) {
            System.out.println("注册过程中出现异常: " + e.getMessage());
            e.printStackTrace();
            System.out.println("=== 注册请求结束 ===");
            model.addAttribute("error", "注册失败: " + e.getMessage());
            return "register";
        }
    }

    // 其他方法保持不变...
}